<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Python数据结构----栈和队列 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Python数据结构----栈和队列</h1>
  <time datetime=2020-06-09T17:22:10&#43;0800 class="post-date">Tue, Jun 9, 2020</time>
  <p><em><strong>栈：先进先出</strong></em></p>
<p><em><strong>队列：先进后出</strong></em></p>
<p><strong>队列</strong></p>
<p>首先，我们来定义一个队列类：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Queue</span>():
 <span style="color:#66d9ef">def</span> __init__(self):
 self<span style="color:#f92672">.</span>__list <span style="color:#f92672">=</span> list()
</code></pre></div><p>接下来，我们给队列类添加一些方法：</p>
<p>•判断队列是否为空</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">isEmpty</span>(self):
	<span style="color:#66d9ef">return</span> self<span style="color:#f92672">.</span>__list <span style="color:#f92672">==</span> []
</code></pre></div><p>•入队</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">push</span>(self, data):
	self<span style="color:#f92672">.</span>__list<span style="color:#f92672">.</span>append(data)
</code></pre></div><p>•出队</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">pop</span>(self):
    <span style="color:#66d9ef">if</span> self<span style="color:#f92672">.</span>isEmpty():
     	<span style="color:#66d9ef">return</span> False
    <span style="color:#66d9ef">return</span> self<span style="color:#f92672">.</span>__list<span style="color:#f92672">.</span>pop(<span style="color:#ae81ff">0</span>)
</code></pre></div><p>•定义len()函数和print()操作类方法</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> __len__(self):
	<span style="color:#66d9ef">return</span> len(self<span style="color:#f92672">.</span>__list)
<span style="color:#66d9ef">def</span> __str__(self):
    <span style="color:#66d9ef">if</span> self<span style="color:#f92672">.</span>isEmpty():
    	<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39;&#39;</span>
    <span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39; &#39;</span><span style="color:#f92672">.</span>join([str(x) <span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> self<span style="color:#f92672">.</span>__list])
</code></pre></div><p><strong>栈</strong></p>
<p>栈的实现和队列类似，同样有入栈和出栈操作</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Stack</span>():
 <span style="color:#66d9ef">def</span> __init__(self):
 self<span style="color:#f92672">.</span>__list <span style="color:#f92672">=</span> list()

 <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">isEmpty</span>(self):
 <span style="color:#66d9ef">return</span> self<span style="color:#f92672">.</span>__list <span style="color:#f92672">==</span> []

 <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">push</span>(self, data):
 self<span style="color:#f92672">.</span>__list<span style="color:#f92672">.</span>append(data)
 
 <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">pop</span>(self):
 <span style="color:#66d9ef">if</span> self<span style="color:#f92672">.</span>isEmpty():
  <span style="color:#66d9ef">return</span> False
 <span style="color:#66d9ef">return</span> self<span style="color:#f92672">.</span>__list<span style="color:#f92672">.</span>pop()

 <span style="color:#66d9ef">def</span> __len__(self):
 <span style="color:#66d9ef">return</span> len(self<span style="color:#f92672">.</span>__list)

 <span style="color:#66d9ef">def</span> __str__(self):
 <span style="color:#66d9ef">if</span> self<span style="color:#f92672">.</span>isEmpty():
  <span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39;&#39;</span>
 <span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39; &#39;</span><span style="color:#f92672">.</span>join([str(x) <span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> self<span style="color:#f92672">.</span>__list])
</code></pre></div><p>栈和队列的类实现基本相同，差别仅在出队和出栈的时候，队列是弹出第一个元素，而则是弹出最后一个元素。这也是队列和堆最本质的区别。</p>

</div>


    </main>

    
  </body>
</html>
